//
//  SMProductListViewController.m
//  ShoppingMall
//
//  Created by Demon on 2018/3/21.
//  Copyright © 2018年 Demon. All rights reserved.
//

#import "SMProductListViewController.h"
#import "SMHomeTableViewCell.h"
#import "SMProductModel.h"
#import "SMPrdDetailViewController.h"

@interface SMProductListViewController () <UITableViewDataSource, UITableViewDelegate>
{
    NSInteger page;
}
@property (weak, nonatomic) IBOutlet UITableView *listTableView;

@property(nonatomic, strong) NSMutableArray *prdListArray;

@end

@implementation SMProductListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.listTableView registerNib:[UINib nibWithNibName:@"SMHomeTableViewCell" bundle:nil] forCellReuseIdentifier:@"listTableViewCell"];
    page = 1;
    [self getProductList];
    [self.listTableView addHeadFresh:self selector:@selector(headerFresh)];
    MJRefreshBackNormalFooter *refreshFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerFresh)];
    refreshFooter.automaticallyChangeAlpha = YES;
    self.listTableView.mj_footer = refreshFooter;
    
}

- (void)headerFresh {
    page = 1;
    [self.listTableView.mj_footer resetNoMoreData];
    [self getProductList];
}

- (void)footerFresh {
    page ++;
    [self getProductList];
}

- (void)getProductList {
    NSString *pageNumber = [NSString stringWithFormat:@"%ld",(long)page];
    [[YDNetClient sharedInstance] getMethod:[NSString stringWithFormat:@"%@&page=%@",self.dataurl,pageNumber] param:nil success:^(id object) {
        [self.listTableView endFresh];
        if (page == 1) [self.prdListArray removeAllObjects];
        NSArray *dicArray = object[@"listData"];
        NSArray *array = [SMProductModel mj_objectArrayWithKeyValuesArray:dicArray];
        if (array.count > 0) {
            [self.prdListArray addObjectsFromArray:array];
        } else {
            [self.listTableView.mj_footer endRefreshingWithNoMoreData];
            if (page > 1) page --;
        }
        [self.listTableView reloadData];
    } fail:^(NSError *error) {
        [self.listTableView endFresh];
        if (page > 1) page --;
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SMPrdDetailViewController *detailVC = MainStoryBoardVC([SMPrdDetailViewController className]);
    SMProductModel *prdModel = self.prdListArray[indexPath.row];
    detailVC.prdId = prdModel.pid;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.prdListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SMHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listTableViewCell" forIndexPath:indexPath];
    SMProductModel *model = self.prdListArray[indexPath.row];
    [cell setHomeTableViewModel:model];
    cell.indexPathCell = indexPath;
    cell.shareBtnClick = ^(NSIndexPath *indexPathCell) {
        [self tableView:tableView didSelectRowAtIndexPath:indexPathCell];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 124;
}

- (NSMutableArray *)prdListArray {
    if (!_prdListArray) {
        _prdListArray = [NSMutableArray array];
    }
    return _prdListArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
